package ru.marketsoft.reminder.threads;

import javax.swing.*;
import java.awt.*;

/**
 * User: dimka3210
 * Date: 28.02.13
 * Time: 21:25
 */
public class ColorOverflow implements Runnable {
    private static JLabel _label;
    private static Color _color;
    private static int ST = 1;

    private static int red = 0;
    private static int green = 0;
    private static int blue = 0;

    public ColorOverflow(JLabel label) {
        _label = label;
        _color = new Color(red, green, blue);
    }

    @Override
    public void run() {
        while (true) {
            switch (ST) {
                case 1:
                    if (red < 255) {
                        red += 1;
                    } else if (green < 255) {
                        green += 1;
                    } else if (blue < 255) {
                        blue += 1;
                    } else {
                        ST = 2;
                    }
                    break;

                case 2:
                    if (red > 0) {
                        red -= 1;
                    } else if (green > 0) {
                        green -= 1;
                    } else if (blue > 0) {
                        blue -= 1;
                    } else {
                        ST = 1;
                    }
                    break;
            }

            try {
                _label.setForeground(new Color(red, green, blue));
                if (!Thread.interrupted()) {
                    Thread.sleep(20);
                }
            } catch (InterruptedException e) {
            }
        }
    }
}
